/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package test.suite.tkaframework.support.component
{

import flash.events.Event;

import org.teotigraphix.dx.core.ToolKitHelper;

import test.suite.tkaframework.support.model.ITestComponentModel;
import test.suite.tkaframework.support.model.TestModel;

/**
 * @copyright Teoti Graphix, LLC
 * @author Michael Schmalle
 * @date 08-24-09
 */
public class TestModelComponent 
	   extends TestComponent
	   implements ITestModelComponent
{
	//--------------------------------------------------------------------------
	//
	//  Public :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  model
	//----------------------------------
	
	private var _model:TestModel;

	/**
	 * @private
	 */
	public function get model():TestModel
	{
		return _model;
	}
	
	/**
	 * @private
	 */
	public function set model(value:TestModel):void
	{
		_model = value;
	}
	
	//----------------------------------
	//  myAutoProperty
	//----------------------------------
	
	// binds this PROPERTY_CHANGE to update the service.myAutoProperty
	// will automatically update the service property so you don't have to create;
	
	//public function set myAutoProperty(value:String):void
	//{
	//	service.myProperty = value;
	//}
	
	// this will autmatically get updated since the Service lists 
	// 'myAutoProperty' as an interest
	
	// although we are creating duplicates of primitive types here
	// references of objects are shared between the Component and Service
	// [Scratch this, figure out the right language] this is much like the 
	// IItemRenderer interface where the data object holds values
	
	// if you are concerned about runtime memory of a service use the 
	// Strategy approach and create the accessor like 'myProperty'
	
	// this duplication of Component to Service comes from needing 
	// the API in mxml attributes
	
	// a Service is not a Model though, it's a serivce layer API defined
	// in a Component, you can also wrap the methods of the service
	// as in the Strategy pattern
	
	// Flex compiler wraps this call and creates a PROPERTY_CHANGE event
	// this adds overhead to file size and call stack at runtime
	
	//--------------------------------------------------------------------------
	// 1) Global PropertyChangeEvent.PROPERTY_CHANGE Method;
	//--------------------------------------------------------------------------
	
	// The service helper listens for the PROPERTY_CHANGE event from the component
	// updates the service, this has overhead of the serivce helper listening to
	// every property change, even though only 1 out of 10 events handled may be 
	// part of the service	
	
	[Bindable]
	[ServiceBinding("test.suite.tkaframework.support.model.ITestComponentModel")]
	public var myAutoProperty:String;
	
	//--------------------------------------------------------------------------
	// 2) Precision PropertyChangeEvent Method;
	//--------------------------------------------------------------------------
	
	// The service helper listens for the explicit 'titleChanged' event 
	// and then updates the service, light overhead in file size 
	// and a lot less at runtime:
	// dispatchEvent() -> handle() -> set service property
	
	//----------------------------------
	//  myPerformanceProperty
	//----------------------------------
	
	/**
	 * @private
	 */
	private var _myPerformanceProperty:String;
	
	[ServiceBinding("test.suite.tkaframework.support.model.ITestComponentModel")]
	[Bindable("myPerformancePropertyChanged")]
	
	/**
	 * //TODO DOCME
	 */
	public function get myPerformanceProperty():String
	{
		return _myPerformanceProperty;
	}
	
	/**
	 * @private
	 */
	public function set myPerformanceProperty(value:String):void
	{
		_myPerformanceProperty = value;
		dispatchEvent(new Event("myPerformanceProperty"));
	}
	
	//--------------------------------------------------------------------------
	// )3 Strategy Method;
	//--------------------------------------------------------------------------
	
	// The serivce property is directly assigned the new value and then the
	// service dispatches a property change event through this component to
	// let listeners know things have changed.
	// This method does not allow for a null service reference on the component
	// where the 2 methods above store the value here and notify the service
	// of a change
	
	// For default framework components, the Strategy method is used since it's
	// the cleanest and most straight forward, just creates one rule that 
	// services cannot be null

	//----------------------------------
	//  myProperty
	//----------------------------------	

	[Bindable]
	[ServiceBinding("test.suite.tkaframework.support.model.ITestComponentModel")]
	public var myProperty:String;
	
//	[Bindable("myPropertyChanged")]
//	
//	/**
//	 * //TODO DOCME
//	 */
//	public function get myProperty():String
//	{
//		return service.myProperty;
//	}
//	
//	/**
//	 * @private
//	 */
//	public function set myProperty(value:String):void
//	{
//		service.myProperty = value;
//	}
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function TestModelComponent()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Called in ToolKitManager.validateInitializationComplete() after 
	 * createChildren() in component's initialize() method.
	 */
	override public function startToolKit():void
	{
		super.startToolKit();
		
		ToolKitHelper.registerModel(
			this, ITestComponentModel, new TestModel() );
	}
}
}